﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace TAM_SPORT
{
   
    public partial class EnterLicense : Form
    {
        private int _close_flag;
        public EnterLicense()
        {
            InitializeComponent();
            try
            {
                this.Closing += new System.ComponentModel.CancelEventHandler(this.CloseAppLication);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            _close_flag = 0;
        }

        public void CloseAppLication(Object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (_close_flag == 0)
            {
                Application.Exit();
            }
        }

        private bool checkA(string strA)
        {
            return CheckLicense.checkA(strA);
        }

        private bool checkB(string strB)
        {
            return CheckLicense.checkB(strB);
        }

        private bool checkDate(string strDate)
        {
            return CheckLicense.checkDate(strDate);
        }

        private bool checkC(string strB)
        {
            return CheckLicense.checkC(strB);
        }

        private bool checkD(string strD)
        {
            return CheckLicense.checkD(strD);
        }
        
        private void btnCapture_Click(object sender, EventArgs e)
        {
            string strA = txtA.Text.Trim();
            string strB = txtB.Text.Trim();
            string strC = txtC.Text.Trim();
            string strD = txtD.Text.Trim();
            if (string.IsNullOrEmpty(strA)
                || string.IsNullOrEmpty(strB)
                || string.IsNullOrEmpty(strC)
                || string.IsNullOrEmpty(strD)
                || strA.Length < 4
                || strB.Length < 5
                || strC.Length < 5
                || strD.Length < 4
                || string.IsNullOrEmpty(txtE.Text)
                || !CheckLicense.checkE(txtE.Text))
            {
                Error er = new Error("Mã license không hợp lệ");
                er.ShowDialog();
                er.Dispose();
            }
            else
            {
                if (checkA(strA) && checkB(strB) && checkC(strC) && checkD(strD))
                {
                    string year = "20" + strA.Substring(1, 2);
                    string month = strB.Substring(3);
                    string day = strC.Substring(3);
                    string expireDate = year + month + day;
                    if (checkDate(expireDate))
                    {
                        if (!ShareMethod.Write(UserConstant.RA, strA + Encrypt.EncryptionToString(strA)) ||
                        !ShareMethod.Write(UserConstant.RB, strB + Encrypt.EncryptionToString(strB)) ||
                        !ShareMethod.Write(UserConstant.RC, strC + Encrypt.EncryptionToString(strC))  ||
                            !ShareMethod.Write(UserConstant.RD, strD + Encrypt.EncryptionToString(strD)))
                        {
                            Error er = new Error("Bạn không có quyền ghi license vào hệ thống. Vui lòng sử dụng quyền admin");
                            er.ShowDialog();
                            er.Dispose();
                        }
                        else
                        {
                            _close_flag = 1;
                            this.Close();
                        }
                    }
                    else
                    {
                        Error er = new Error("Mã license không hợp lệ");
                        er.ShowDialog();
                        er.Dispose();
                    }
                }
                else
                {
                    Error er = new Error("Mã license không hợp lệ");
                    er.ShowDialog();
                    er.Dispose();
                }
            }
        }
    }
}
